package cn.demomaster.quickui_sample.service;

import android.content.Context;
import android.net.TrafficStats;
import android.os.Build;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.demomaster.quickcompat.service.QuickFloatingService;
import com.demomaster.quickcompat.service.ServiceHelper;
import com.demomaster.quickcompat.util.QuickHandler;
import com.demomaster.quickcompat.util.system.QDAppInfoUtil;

import java.lang.ref.WeakReference;

import cn.demomaster.quicklogger.QdLog;
import cn.demomaster.quickui.helper.NetworkStatsHelper;
import cn.demomaster.quickui.helper.TrafficHelper;
import cn.demomaster.quickui_sample.R;

/**
 * 流量悬浮
 * Created
 */
public class TrafficFloatingService extends QuickFloatingService {
    TextView tv_up;
    TextView tv_down;
    TextView tv_today;
    NetworkStatsHelper networkStatsHelper;
    Context mContext;

    @Override
    public void onCreateView(Context context, WindowManager windowManager) {
        mContext = context.getApplicationContext();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_floating_traffic, null);
        view.findViewById(R.id.iv_close).setOnClickListener(v -> ServiceHelper.stopService(context,TrafficFloatingService.class));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            networkStatsHelper = new NetworkStatsHelper(mContext);
        }
        tv_up = view.findViewById(R.id.tv_up);
        tv_down = view.findViewById(R.id.tv_down);
        tv_today = view.findViewById(R.id.tv_today);
        handler.postDelayed(runnable, 0);
        addView(view,new WindowManager.LayoutParams(), true, true, 0, 0); ;
    }

    int uid;
    Handler handler = new QuickHandler();
    public static class CRunnable implements Runnable{

        WeakReference<TrafficFloatingService> trafficFloatingServiceWeakReference ;
        public CRunnable(TrafficFloatingService trafficFloatingService) {
            trafficFloatingServiceWeakReference = new WeakReference<>(trafficFloatingService);
        }

        @Override
        public void run() {
            if(trafficFloatingServiceWeakReference!=null&&trafficFloatingServiceWeakReference.get()!=null) {
                String str1 = "";
                long value;
                if (trafficFloatingServiceWeakReference.get().tv_up != null) {
                    value = TrafficHelper.getInstant().getUpdateSpeed();
                    str1 = trafficFloatingServiceWeakReference.get().fomatString(value);
                    trafficFloatingServiceWeakReference.get().tv_up.setText(str1);
                }
                if (trafficFloatingServiceWeakReference.get().tv_down != null) {
                    value = TrafficHelper.getInstant().getDownloadSpeed();
                    str1 = trafficFloatingServiceWeakReference.get().fomatString(value);
                    trafficFloatingServiceWeakReference.get().tv_down.setText(str1);
                }

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    trafficFloatingServiceWeakReference.get().uid = QDAppInfoUtil.getUidByPackageName(trafficFloatingServiceWeakReference.get().mContext, trafficFloatingServiceWeakReference.get().mContext.getPackageName());
                    QdLog.d("uid1=" + trafficFloatingServiceWeakReference.get().uid);
                    //value = networkStatsHelper.getAllBytesWifi(mContext);
                    value = TrafficStats.getTotalRxBytes() + TrafficStats.getTotalTxBytes();
                    str1 = "total:" + trafficFloatingServiceWeakReference.get().fomatString(value);
                    trafficFloatingServiceWeakReference.get().tv_today.setText(str1);
                }

                trafficFloatingServiceWeakReference.get().handler.postDelayed(trafficFloatingServiceWeakReference.get().runnable, 1000);
            }
        }
    }
    Runnable runnable = new CRunnable(this);
    private String fomatString(long value) {
        String str1 = "";
        if (value > 1073741824) {
            str1 = value / 1073741824 + "gb";
        } else if (value > 1048576) {
            str1 = value / 1048576 + "mb";
        } else if (value > 1024) {
            str1 = value / 1024 + "kb";
        } else if (value < 1024) {
            str1 = value + "bit";
        }
        return str1;
    }

}
